package com.lujianfei.powersearch.ui.createOrUpdate.widget.imageviewedit.adapter

import android.net.Uri
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lujianfei.powersearch.R
import com.lujianfei.powersearch.base.MovableAdapter
import com.lujianfei.powersearch.ui.createOrUpdate.widget.imageviewedit.adapter.viewholder.ImageViewEditAdapterViewHolder
import com.lujianfei.powersearch.ui.createOrUpdate.widget.imageviewedit.model.ImageViewEditItem
import com.lujianfei.powersearch.utils.ImageUtils
import com.lujianfei.powersearch.utils.SystemUtils
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

class ImageViewEditAdapter: MovableAdapter<ImageViewEditItem, RecyclerView.ViewHolder>() {

    var onAddClickListener:(()->Unit) ?= null
    var onRemoveClickListener:((ImageViewEditItem)->Unit) ?= null
    var onPhotoClickListener:((ImageViewEditItem)->Unit) ?= null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ImageViewEditAdapterViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.imageview_edit_item, parent, false))
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ImageViewEditAdapterViewHolder) {
            val itemModel = datas[position]
            holder.itemView.setOnClickListener {
                if (itemModel.type == ImageViewEditItem.TYPE_ADD) {
                    onAddClickListener?.invoke()
                } else {
                    onPhotoClickListener?.invoke(itemModel)
                }
            }
            holder.bind(itemModel) {
                onRemoveClickListener?.invoke(it)
                val index = datas.indexOf(it)
                datas.removeAt(index)
                notifyItemRemoved(index)
            }
        }
    }

    override fun onItemMove(fromPosition: Int, toPosition: Int):Boolean {
        //交换位置
        val items = datas
        val item = items[fromPosition]
        val itemTo = items[toPosition]
        if (item.type != ImageViewEditItem.TYPE_NORMAL || itemTo.type != ImageViewEditItem.TYPE_NORMAL) {
            return false
        }
        items.removeAt(fromPosition)
        items.add(toPosition, item)
        notifyItemMoved(fromPosition, toPosition)
        return true
    }

    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?) {
    }

    override fun clearView(viewHolder: RecyclerView.ViewHolder?) {
    }

    fun addData(uriString: Uri?) {
        uriString?.let {
            val insertedIdx = datas.size - 1
            datas.add(
                insertedIdx,
                ImageViewEditItem(imageUri = it, type = ImageViewEditItem.TYPE_NORMAL)
            )
            notifyItemInserted(insertedIdx)
        }
    }

    fun addData(uriString: String?) {
        uriString?.let {
            val insertedIdx = datas.size - 1
            datas.add(
                insertedIdx,
                ImageViewEditItem(imageUrl = it, type = ImageViewEditItem.TYPE_NORMAL)
            )
            notifyItemInserted(insertedIdx)
        }
    }

    fun getFinalImagePath(): String {
        return datas.filter { it.type == ImageViewEditItem.TYPE_NORMAL }.map { if (it.hasUri()) it.imageUri.toString() else it.imageUrl }.joinToString(",")
    }
}